//
//@File    :	15.9_error3.cpp
//@Time    :	2022/03/10 11:31:10
//@Auther  :	Sun Sheng
//@Contact :	970340843@qq.com
//@Desc    :	using an exception
//

//使用异常机制处理错误
//1.引发异常   throw
//2.使用处理程序捕获异常   catch
//3.使用try块  try

#include <iostream>

double hmean(double a,double b);

int main()
{
    double x,y,z;

    std::cout<<"Enter two numters: ";
    while (std::cin>>x>>y)
    {
        try
        {
            z=hmean(x,y);
        }
        catch(const char *s)
        {
            std::cout<<s<<std::endl;
            std::cout<<"Enter a new pair of numbers: ";
            continue;
        }
        std::cout<<"Harmonic mean od "<<x<<" and "<<y<<" is "<<z<<std::endl;
        std::cout<<"Enter next set of numbers <q to quit>: ";
    }
    std::cout<<"Bye!\n";
    system("pause");
    return 0;
}

double hmean(double a,double b){
    if (a==-b)
    {
       throw "bad hmean() arguments:a=-b not allowed";
    }
    return 2.0*a*b/(a+b);
}
